import 'dart:async';
import 'dart:typed_data';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter_syt/starter/env_provider.dart';
import 'package:flutter_syt/starter/platform/platform_processor.dart';
import 'package:flutter_syt/starter/registry.dart';
import 'package:flutter_syt/starter/starter_exception.dart';
import 'package:flutter_syt/starter/storage/storage.dart';

class AppContext {
  final List<Registry> _registry = [];

  EnvProvider? _provider;

  Storage? _defaultKVStorage;

  ChannelApi? _channelApi;

  BuildContext? context;

  // WebViewController? webController;

  set storage(Storage value) {
    if (_defaultKVStorage != null) {
      throw IllegalStateException("");
    }
    _defaultKVStorage = value;
  }

  Storage get storage => _defaultKVStorage!;

  EnvProvider get provider => _provider!;

  set provider(EnvProvider value) {
    _provider = value;
  }

  List<Registry> get registry => _registry;

  bool isReleaseMode =
      bool.fromEnvironment('dart.vm.product', defaultValue: false);
  bool isProfileMode =
      bool.fromEnvironment('dart.vm.profile', defaultValue: false);

  bool get isDebugMode => !isReleaseMode && !isProfileMode;

  RegExp emojiRegexp = RegExp(
      "[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]");

  @protected
  set registry(List<Registry> value) {
    _registry.addAll(value);
  }

  ChannelApi get channelApi => _channelApi!;

  set channelApi(ChannelApi value) {
    _channelApi = value;
  }

  List<int>? _cerBytes;
  List<int> get cerBytes => _cerBytes!;
  void setCerBytes(Uint8List value) {
    _cerBytes = value;
  }

  PackageInfo? _packageInfo;
  PackageInfo get packageInfo => _packageInfo!;

  void setPackageInfo(PackageInfo value) {
    _packageInfo = value;
  }

  ///服务器和本地时间差  秒
  int timeStamp = 0;

  bool _loadUser = false;
  bool get isLoadUser => _loadUser;

  void setLoadUser() {
    _loadUser = true;
  }

  AndroidDeviceInfo? _andriodInfo;
  AndroidDeviceInfo get andriodInfo => _andriodInfo!;

  void setAndriodInfo(AndroidDeviceInfo value) {
    _andriodInfo = value;
  }

  IosDeviceInfo? _iOSInfo;
  IosDeviceInfo get iOSInfo => _iOSInfo!;

  void setIOSInfo(IosDeviceInfo value) {
    _iOSInfo = value;
  }

  ///设备
  // String deviceBrand = '';

  ///手机系统版本
  // String deviceVersion = '';

  ///网络
  // String deviceNetwork = '';

  ///地区
  // String deviceLocation = '';

  Map? permissionData;

  // var deviceData = await DeviceInfoPlugin().androidInfo;
  // String deviceId = '';
  ConnectivityResult connectionStatus = ConnectivityResult.none;
  Connectivity connectivity = Connectivity();
  bool isRestartService = true;
  bool isRestartMallService = true;
  // Stream<List<ConnectivityResult>>
  late StreamSubscription<List<ConnectivityResult>> connectivitySubscription;

  final RouteObserver<ModalRoute> rootObserver = RouteObserver<ModalRoute>();
}
